package com.keyinong.restaurant.dapter;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.bluetooth.BluetoothActivity;
import com.keyinong.restaurant.R;
import com.keyinong.restaurant.entity.OrderEntity;
import com.keyinong.restaurant.tools.HttpTools;
import com.loopj.android.http.JsonHttpResponseHandler;

public class OrderAdapter extends BaseAdapter{
	Context context;
	ArrayList<OrderEntity> list;
	HttpTools httpTools=null;
	String token;
	View iscatview = null;
	
	public OrderAdapter(Context context,
			ArrayList<OrderEntity> list) {
		super();
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@SuppressLint("ResourceAsColor")
	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		final Handler handler;
		if (view == null) {
			handler = new Handler();
			view = LayoutInflater.from(context).inflate(
					R.layout.item_order, null);
			handler.tv_time=(TextView)view.findViewById(R.id.tv_time);
			handler.tv_user=(TextView)view.findViewById(R.id.tv_user);
			handler.tv_address=(TextView)view.findViewById(R.id.tv_address);
			handler.tv_phone=(TextView)view.findViewById(R.id.tv_phone);
			handler.tv_state=(TextView)view.findViewById(R.id.tv_state);
			handler.btn_queren=(Button)view.findViewById(R.id.btn_queren);
			handler.btn_dayin=(Button)view.findViewById(R.id.btn_dayin);
			handler.btn_xiangqing=(Button)view.findViewById(R.id.btn_xiangqiang);
			handler.tv_qucanfangshi=(TextView)view.findViewById(R.id.tv_qucanfangshi);
			handler.tv_paymode=(TextView)view.findViewById(R.id.tv_paymode);
			SharedPreferences sp = context.getSharedPreferences("spReg",
					Context.MODE_PRIVATE);
			token = sp.getString("token", "");
			view.setTag(handler);
		} else {
			handler = (Handler) view.getTag();
		}
		handler.tv_time.setText(list.get(position).getReceive_time());
		handler.tv_phone.setText(list.get(position).getPhone());
		handler.tv_user.setText(list.get(position).getName());
		if(list.get(position).getState().equals("1")){
			handler.tv_state.setText("待确认");
			handler.btn_queren.setVisibility(View.VISIBLE);
			handler.btn_xiangqing.setVisibility(View.INVISIBLE);
		}else if(list.get(position).getState().equals("2")){
			handler.tv_state.setText("待取餐");
			//			handler.btn_queren.setText("已接单");
			//			handler.btn_queren.setBackgroundResource(R.drawable.red);
			handler.btn_queren.setVisibility(View.INVISIBLE);
			handler.btn_xiangqing.setVisibility(View.VISIBLE);
		}else if(list.get(position).getState().equals("3")){
			handler.tv_state.setText("已完成");
			//			handler.btn_queren.setText("已完成");
			//			handler.btn_queren.setEnabled(false);
			handler.btn_queren.setVisibility(View.INVISIBLE);
			handler.btn_xiangqing.setVisibility(View.INVISIBLE);
//			handler.btn_xiangqing.setEnabled(false);
		}
		if (list.get(position).getAddress().equals("null")||list.get(position).getAddress().equals("")) {
			handler.tv_address.setText("无");
		}else {
			handler.tv_address.setText(list.get(position).getAddress());
		}
		
		if(list.get(position).getRecmode().equals("1")){
			handler.tv_qucanfangshi.setText("店内自提");
			handler.tv_qucanfangshi.setTextColor(Color.parseColor("#000000"));

		}else if (list.get(position).getRecmode().equals("2")){
			handler.tv_qucanfangshi.setText("送货上门");
			handler.tv_qucanfangshi.setTextColor(Color.parseColor("#bb4e4f"));
		}

		
		if(list.get(position).getPaymodeid().equals("1")){
			handler.tv_paymode.setText("支付宝");
			handler.tv_paymode.setTextColor(Color.parseColor("#000000"));
		}else if(list.get(position).getPaymodeid().equals("2")){
			handler.tv_paymode.setText("微信");
			handler.tv_paymode.setTextColor(Color.parseColor("#000000"));
		}else if(list.get(position).getPaymodeid().equals("3")){
			handler.tv_paymode.setText("余额");
			handler.tv_paymode.setTextColor(Color.parseColor("#000000"));
		}else if(list.get(position).getPaymodeid().equals("4")){
			handler.tv_paymode.setText("店内支付");
			handler.tv_paymode.setTextColor(Color.parseColor("#bb4e4f"));
		}else if(list.get(position).getPaymodeid().equals("5")){
			handler.tv_paymode.setText("刷卡");
			handler.tv_paymode.setTextColor(Color.parseColor("#000000"));
		}else if(list.get(position).getPaymodeid().equals("6")){
			handler.tv_paymode.setText("线下支付宝");
			handler.tv_paymode.setTextColor(Color.parseColor("#000000"));
		}else if(list.get(position).getPaymodeid().equals("7")){
			handler.tv_paymode.setText("线下微信");
			handler.tv_paymode.setTextColor(Color.parseColor("#000000"));
		}else if(list.get(position).getPaymodeid().equals("8")){
			handler.tv_paymode.setText("现金");
			handler.tv_paymode.setTextColor(Color.parseColor("#000000"));
		}
		
		handler.btn_xiangqing.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(list.get(position).getPaymodeid().equals("4")){
					paymodedialog(position);
				}else {
					querendialog(position);
				}
			}
		});
		handler.btn_dayin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(context, BluetoothActivity.class);
				intent.putExtra("id", list.get(position).getId());
				context.startActivity(intent);
			}
		});
		
		handler.btn_queren.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (httpTools == null) {
					httpTools = new HttpTools(context);
				}
				httpTools.yiwancheng(token, list.get(position).getId(),"2",list.get(position).getPaymodeid(),
						new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						super.onSuccess(statusCode, headers, response);
						try {
							boolean code = response.getBoolean("code");
							String msg = response.getString("msg");
//							Log.i("zq", "adapter---"+response.toString());
							handler.btn_queren.setVisibility(View.INVISIBLE);
							if (code == true) {
								Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
								handler.btn_xiangqing.setVisibility(View.VISIBLE);
								//										view.setBackgroundResource(R.drawable.shouchang1);
							} else {
								Toast.makeText(context, "已接单", Toast.LENGTH_SHORT).show();
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				});

			}
		});
		return view;
	}
	class Handler {
		TextView tv_time;
		TextView tv_user;
		TextView tv_address;
		TextView tv_phone;
		TextView tv_state;
		TextView tv_qucanfangshi;
		TextView tv_paymode;
		Button btn_queren;
		Button btn_dayin;
		Button btn_xiangqing;
	}
	
	/**确定订单*/
	public void querendingdan(final int index){
		if (httpTools == null) {
			httpTools = new HttpTools(context);
		}
		httpTools.yiwancheng(token, list.get(index).getId(),"3",list.get(index).getPaymodeid(),
				new JsonHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					boolean code = response.getBoolean("code");
					String msg = response.getString("msg");
					if (code == true) {
						Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
						//									btn_queren.setText("订单已完成");
						//									Intent intent = new Intent();
						//									intent.setClass(getApplicationContext(), ScanActivity.class);
						//									startActivity(intent);
						//										view.setBackgroundResource(R.drawable.shouchang1);
						//									btn_queren.setEnabled(false);
						
					} else {
						Toast.makeText(context, "该订单已完成！", Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		});

	}
	
	
	/**是否确定订单弹窗*/
	private void querendialog(final int index) {
		
		final Dialog dialog = new Dialog(context);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		iscatview = LayoutInflater.from(context).inflate(
				R.layout.dialog_phone, null);
		TextView tital = (TextView) iscatview.findViewById(R.id.tital);
		//			TextView tv_phone = (TextView) iscatview.findViewById(R.id.tv_phone);
		TextView tv_no = (TextView) iscatview.findViewById(R.id.tv_no2);
		TextView tv_yes = (TextView) iscatview.findViewById(R.id.tv_yes2);
		tital.setText("是否确认订单？");
		dialog.setContentView(iscatview);
		dialog.show();
		tv_no.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		tv_yes.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				orderDetail(index);
				querendingdan(index);
				dialog.dismiss();
			}
		});
	}
	
	
	
	/**支付方式弹窗*/
	private void paymodedialog(final int index) {

		final Dialog dialog = new Dialog(context);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

		iscatview = LayoutInflater.from(context).inflate(
				R.layout.dialog_paymode, null);
		//			TextView tital = (TextView) iscatview.findViewById(R.id.tital);
		//			TextView tv_phone = (TextView) iscatview.findViewById(R.id.tv_phone);
		TextView tv_shuaka = (TextView) iscatview.findViewById(R.id.tv_shuaka);
		TextView tv_zhifubao = (TextView) iscatview.findViewById(R.id.tv_zhifubao);
		TextView tv_weixin = (TextView) iscatview.findViewById(R.id.tv_weixin);
		TextView tv_xianjin = (TextView) iscatview.findViewById(R.id.tv_xianjin);
		//			tv_phone.setText(phone);
		//			tv_phone.setVisibility(View.GONE);
		//			tital.setText("该上下班有人加入，拼车是否完成?");
		dialog.setContentView(iscatview);
		dialog.show();
		tv_shuaka.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (httpTools == null) {
					httpTools = new HttpTools(context);
				}
				httpTools.yiwancheng(token, list.get(index).getId(),"3","5",
						new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						super.onSuccess(statusCode, headers, response);
						try {
							boolean code = response.getBoolean("code");
							String msg = response.getString("msg");
							if (code == true) {
								Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
								//								Intent intent = new Intent();
								//								intent.setClass(getApplicationContext(), MainActivity.class);
								//								startActivity(intent);
								//								finish();
								//										view.setBackgroundResource(R.drawable.shouchang1);
							} else {
								Toast.makeText(context, "订单已完成！", Toast.LENGTH_SHORT).show();
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				});
				dialog.dismiss();
			}
		});
		tv_zhifubao.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (httpTools == null) {
					httpTools = new HttpTools(context);
				}
				httpTools.yiwancheng(token, list.get(index).getId(),"3","6",
						new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						super.onSuccess(statusCode, headers, response);
						try {
							boolean code = response.getBoolean("code");
							String msg = response.getString("msg");
							if (code == true) {
								Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
								//								Intent intent = new Intent();
								//								intent.setClass(getApplicationContext(), MainActivity.class);
								//								startActivity(intent);
								//								finish();
								//										view.setBackgroundResource(R.drawable.shouchang1);
							} else {
								Toast.makeText(context, "订单已完成！", Toast.LENGTH_SHORT).show();
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				});
				dialog.dismiss();
			}
		});
		tv_weixin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (httpTools == null) {
					httpTools = new HttpTools(context);
				}
				httpTools.yiwancheng(token, list.get(index).getId(),"3","7",
						new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						super.onSuccess(statusCode, headers, response);
						try {
							boolean code = response.getBoolean("code");
							String msg = response.getString("msg");
							if (code == true) {
								Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
								//								Intent intent = new Intent();
								//								intent.setClass(context, MainActivity.class);
								//								startActivity(intent);
								//								finish();
								//										view.setBackgroundResource(R.drawable.shouchang1);
							} else {
								Toast.makeText(context, "订单已完成！", Toast.LENGTH_SHORT).show();
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				});
				dialog.dismiss();
			}
		});
		tv_xianjin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (httpTools == null) {
					httpTools = new HttpTools(context);
				}
				httpTools.yiwancheng(token, list.get(index).getId(),"3","8",
						new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						super.onSuccess(statusCode, headers, response);
						try {
							boolean code = response.getBoolean("code");
							String msg = response.getString("msg");
							if (code == true) {
								Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
								//								Intent intent = new Intent();
								//								intent.setClass(context, MainActivity.class);
								//								context.startActivity(intent);
								//								finish();
								//										view.setBackgroundResource(R.drawable.shouchang1);
							} else {
								Toast.makeText(context, "订单已完成！", Toast.LENGTH_SHORT).show();
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}

				});
				dialog.dismiss();
			}
		});
	}
	
	
}
